<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="文件名称" prop="fileName">
        <el-input
          v-model="queryParams.fileName"
          placeholder="请输入文件名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dmsIms:file:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dmsIms:file:edit']"
          >修改</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dmsIms:file:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dmsIms:file:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="fileList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="60" />
      <el-table-column
        label="文件名称"
        align="center"
        prop="fileName"
        show-overflow-tooltip
      />
      <el-table-column label="文件类型" align="center" prop="fileType" />
      <el-table-column
        label="文件路径"
        align="center"
        prop="filePath"
        show-overflow-tooltip
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleDownload(scope.row)"
            >下载</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dmsIms:file:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改测试文件对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <div class="app-container" style="background-color: white">
        <el-upload
          ref="upload"
          class="upload-demo"
          action="/dmsIms/file/uploadFiles"
          :on-remove="handleRemove"
          :file-list="fileLists"
          :auto-upload="false"
          :on-change="fileChange"
          :limit="10"
          :on-exceed="onExceed"
          multiple
          drag
        >
          <i class="el-icon-upload" />
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div slot="tip" class="el-upload__tip">
            1、上传不超过10M的文件, 且一次最多上传10个文件
          </div>
          <div slot="tip" class="el-upload__tip">
            2、上传重复文件会添加文件前缀
          </div>
        </el-upload>
        <el-button
          v-loading.fullscreen.lock="fullscreenLoading"
          style="margin-top: 5px; margin-left: 300px"
          type="primary"
          element-loading-text="文件上传中"
          element-loading-background="rgba(0, 0, 0, 0.8)"
          @click="submitUpload"
          v-preventReClick
        >
          提交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listFile,
  getFile,
  delFile,
  addFile,
  updateFile,
  fileUploadFiles,
  checkFileExist,
} from "@/api/dmsIms/file";
import request from "@/utils/request";

export default {
  name: "File",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测试文件表格数据
      fileList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fileName: null,
        fileType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      fileLists: [],
      fullscreenLoading: false,
      fileDataList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询测试文件列表 */
    getList() {
      this.loading = true;
      listFile(this.queryParams).then((response) => {
        this.fileList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.fileLists = [];
      this.form = {
        id: null,
        fileName: null,
        fileType: null,
        filePath: null,
        status: 0,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加测试文件";
    },
    /** 修改按钮操作 */
    // handleUpdate(row) {
    //   this.reset();
    //   const id = row.id || this.ids;
    //   getFile(id).then((response) => {
    //     this.form = response.data;
    //     this.open = true;
    //     this.title = "修改测试文件";
    //   });
    // },
    /** 下载按钮操作 */
    handleDownload(row) {
      checkFileExist(row.id).then((response) => {
        if (response.code === 200) {
          window.location.href =
            // `http://localhost:8088/dmsIms/file/download/` + row.id;
            `http://localhost:8080/dmsIms/file/download/` + row.id;
          // `http://172.16.200.3:8088/dmsIms/file/download/` + row.id;
        }
      });
      // console.log(process.env.VUE_APP_BASE_API);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateFile(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFile(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除测试文件编号为"' + ids + '"的数据项？')
        .then(function () {
          return delFile(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "dmsIms/file/export",
        {
          ...this.queryParams,
        },
        `file_${new Date().getTime()}.xlsx`
      );
    },
    handleRemove(file, fileList) {
      if (fileList.length > 0) {
        this.fileLists = fileList;
      } else {
        this.fileLists = [];
      }
    },
    fileChange(file, fileList) {
      if (file.size / 1024 / 1024 < 10) {
        this.fileLists.push(file);
      } else {
        this.$refs.upload.uploadFiles.splice(
          this.$refs.upload.uploadFiles.length - 1,
          1
        );
        // this.$message.error("文件大小超过10M，不能添加");
      }
    },
    onExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 10 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );

      // files.forEach((file) => {
      //   if (file.size / 1024 / 1024 < 10 && this.fileLists.length < 10) {
      //     file.status = "ready";
      //     this.fileLists.push(file);
      //   }
      // });
    },
    submitUpload() {
      // console.log(this.fileLists);
      // this.fullscreenLoading = true;
      //  用FormData存放上传文件
      let formData = new FormData();
      this.fileLists.forEach((res) => {
        let tempFileData = {};
        formData.append("file", res.raw);
        tempFileData.fileType = res.type;
        this.fileDataList.push(tempFileData);
      });
      // fileUploadFile(formData).then((response) => {
      //   console.log(response);
      // });
      fileUploadFiles(formData).then((response) => {
        // this.res = response.code;
        if (response.code === 200) {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        } else {
          this.open = false;
          this.getList();
        }
      });
    },
  },
};
</script>
<style>
.upload-demo > ul {
  max-height: 300px;
  overflow: auto;
  width: 360px;
}
</style>
